package com.example.databinding_demo.mvvm;

import android.app.Application;
import android.view.View;

import androidx.databinding.BaseObservable;
import androidx.databinding.Bindable;

import com.example.databinding_demo.BR;
import com.example.databinding_demo.MCallback;
import com.example.databinding_demo.User;
import com.example.databinding_demo.databinding.ActivityMvvmBinding;

public class MvvmViewModel extends BaseObservable {

    private MvvmModel mvvmModel;
    private String result;
    private String editText;

    @Bindable
    public String getResult() {
        return result;
    }

    public void setResult(String result) {
        this.result = result;
        notifyPropertyChanged(BR.result);
    }

    public MvvmViewModel(Application application) {
        mvvmModel = new MvvmModel();
    }

    @Bindable
    public String getEditText() {
        return editText;
    }

    public void setEditText(String editText) {
        this.editText = editText;
        notifyPropertyChanged(BR.editText);
    }

    /**
     * 获取数据
     * @param view
     */
    public void getData(View view){
        mvvmModel.getAccountData(editText, new MCallback<User>() {
            @Override
            public void onSuccess(User user) {
                String info = user.getUsername() + "/" + user.getPwd();
                setResult(info);
            }

            @Override
            public void onFailed() {
                setResult("数据请求失败。。。");
            }
        });
    }
}
